<template>
  <div ref="wrapEl">
    <AdTable @register="registerTable">
      <template #toolbar>
        <Authority>
          <BasicUpload
            :maxSize="1024"
            :maxNumber="10"
            :emptyHidePreview="true"
            @change="handleChange"
            :uploadParams="{}"
            :api="uploadApi"
          />
        </Authority>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'ant-design:copy-outlined',
                tooltip: '复制路径',
                color: 'success',
                onClick: handleCopy.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                tooltip: '删除',
                auth: '', // 权限控制
                ifShow: (_action) => {
                  return true;
                },
                popConfirm: {
                  title: '确认删除？',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </AdTable>
  </div>
</template>

<script lang="ts">
  import { defineComponent, ref, unref } from 'vue';
  import { Authority } from '@/components/Authority';
  import { AdTable, useAdTable, TableAction } from '@/components/AdTable';
  import { useLoading } from '@/components/Loading';
  import { useMessage } from '@/hooks/web/useMessage';
  import { FormModeEnum } from '@/enums/formEnum';
  import { AjaxResultTypeEnum } from '@/enums/ajaxResultTypeEnum';
  import { useCopyToClipboard } from '@/hooks/web/useCopyToClipboard';
  import Icon from '@/components/Icon/Icon.vue';
  import { BasicUpload } from '@/components/Upload';
  import { uploadApi } from '@/api/sys/upload';
  import { columns } from './file.data';
  import { Read, Delete } from './file.api';

  export default defineComponent({
    name: 'FileManagePage',
    components: {
      Authority,
      AdTable,
      TableAction,
      Icon,
      BasicUpload,
    },
    setup() {
      const { createMessage } = useMessage();
      const { clipboardRef, copiedRef } = useCopyToClipboard();
      const wrapEl = ref<ElRef>(null);
      const [openWrapLoading, closeWrapLoading] = useLoading({
        target: wrapEl,
        props: {
          tip: '加载中...',
          absolute: true,
        },
      });

      const [registerTable, { openDetailForm, reload }] = useAdTable({
        // 表标题呈现器
        title: '文件列表',
        // 表格数据接口请求对象
        api: Read,
        // 详情表单抬头
        detailForm: {
          // 表单标识
          formIndex: 'file_file_detail',
          // 主键字段
          keyField: 'Id',
          title: '文件详情',
          // createApi: Create,
          // updateApi: Update,
          // deleteApi: Delete,
        },
        // 表单列信息 BasicColumn[]
        columns,
        // 是否可拖拽列
        canColDrag: true,
        // 使用搜索表单
        useSearchForm: true,
        // 显示表格设置工具
        showTableSetting: true,
        //表格设置项
        tableSetting: { redo: true, size: true, setting: true, fullScreen: true },
        // 是否显示表格边框
        bordered: false,
        // 是否显示序号列
        showIndexColumn: true,
        // 表格右侧操作列配置 BasicColumn
        actionColumn: {
          width: 125,
          title: '操作',
          dataIndex: 'action',
          fixed: 'right',
        },
      });

      function handleChange() {
        reload();
      }

      /** 处理删除 */
      async function handleDelete(record: Recordable) {
        const ajaxResult = await Delete(record.Id);
        if (ajaxResult.Type === AjaxResultTypeEnum.Success) {
          if (ajaxResult.Content) {
            createMessage.success(ajaxResult.Content);
          }
          reload();
        }
      }

      /** 复制 */
      function handleCopy(record: Recordable) {
        clipboardRef.value = record.RequestPath;
        if (unref(copiedRef)) {
          createMessage.success('复制路径成功');
        }
      }

      return {
        wrapEl,
        registerTable,
        handleDelete,
        handleChange,
        uploadApi,
        handleCopy,
      };
    },
  });
</script>
